package com.jxmcloud.business.cut.controller.admin.activity;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.jxmcloud.business.cut.constants.CutApi;
import com.jxmcloud.business.cut.dto.admin.AdminUserMergerDto;
import com.jxmcloud.business.cut.dto.admin.GmUserWatchLiveTaskDetailDto;
import com.jxmcloud.business.cut.dto.gm.AddInvitedDto;
import com.jxmcloud.business.cut.dto.gm.KickOffDto;
import com.jxmcloud.business.cut.dto.gm.ResetActivityIdDto;
import com.jxmcloud.business.cut.dto.gm.UpdateProgressDto;
import com.jxmcloud.business.cut.model.activity.Bag;
import com.jxmcloud.business.cut.service.admin.activity.IGmService;
import com.jxmcloud.business.cut.service.app.system.ISystemService;
import com.jxmcloud.business.cut.service.database.IUserLoginInfoDBService;
import com.jxmcloud.business.cut.service.user.IUserBaseService;
import com.jxmcloud.business.cut.service.user.IUserManagerService;
import com.jxmcloud.business.cut.vo.*;
import com.xlcloud.platform.api.exception.BusinessException;
import com.xlcloud.platform.util.RUtil;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * Web端砍一刀活动控制器
 */
@Validated
@Slf4j
@RestController
@RequestMapping(CutApi.NAH_CUT_ACTIVITY)
public class GmController {

    @Resource
    private IGmService iGmService;
    @Resource
    private IUserManagerService userManagerService;
    @Resource
    private IUserBaseService userBaseService;
    @Resource
    private IUserLoginInfoDBService userLoginInfoDBService;
    @Autowired
    private RedissonClient redissonClient;
    @Autowired
    private ISystemService systemService;

    /**
     * 调整当前流程
     * @param updateProgressDto
     * @return 结果
     */
    @PostMapping("/updateProgress")
    public String updateProgress(@RequestBody UpdateProgressDto updateProgressDto) {
        try {
            iGmService.updateProgressNew(updateProgressDto);
            return RUtil.appSuccess("成功");
        } catch (BusinessException e) {
            log.error("请求失败{}", e);
            return RUtil.appFail(e.getMessage());
        } catch (Exception e) {
            log.error("请求失败{}", e);
            return RUtil.appFail("请求失败!");
        }
    }

    /**
     * 用户封禁
     * @param dto
     * @return 结果
     */
    @PostMapping("/updateUserStatus")
    public String updateUserStatus(@RequestBody @Validated AdminUserMergerDto dto) {
        try {
            userManagerService.updateUserStatus(dto);
            return RUtil.appSuccess("修改成功");
        } catch (BusinessException e) {
            log.error("请求失败{}", e);
            return RUtil.appFail(e.getMessage());
        } catch (Exception e) {
            log.error("请求失败{}", e);
            return RUtil.appFail("请求失败!");
        }
    }

    /**
     * 给用户添加助力用户 （新增一个邀请 A邀请B）
     * @param addInvitedDto
     * @return 结果
     */
    @PostMapping("/checkAddInvitedPlayer")
    public String checkAddInvitedPlayer(@RequestBody AddInvitedDto addInvitedDto) {
        try {
            iGmService.checkAddInvitedPlayer(addInvitedDto);
            return RUtil.appSuccess("成功");
        } catch (BusinessException e) {
            log.error("请求失败{}", e);
            return RUtil.appFail(e.getMessage());
        } catch (Exception e) {
            log.error("请求失败{}", e);
            return RUtil.appFail("请求失败!");
        }
    }

    /**
     * 重置用户活动ID
     * @param resetActivityIdDto
     * @return 结果
     */
    @PostMapping("/resetActivityId")
    public String resetActivityId(@RequestBody ResetActivityIdDto resetActivityIdDto) {
        try {
            iGmService.resetActivityId(resetActivityIdDto);
            return RUtil.appSuccess("成功");
        } catch (BusinessException e) {
            log.error("请求失败{}", e);
            return RUtil.appFail(e.getMessage());
        } catch (Exception e) {
            log.error("请求失败{}", e);
            return RUtil.appFail("请求失败!");
        }
    }

    /**
     * 重置用户活动
     * @param
     * @return 结果
     */
    @PostMapping("/resetUserFlowInfo")
    public String resetUserFlowInfo(Integer activityId) {
        try {
            iGmService.resetUserFlowInfo(activityId);
            return RUtil.appSuccess("成功");
        } catch (BusinessException e) {
            log.error("请求失败{}", e);
            return RUtil.appFail(e.getMessage());
        } catch (Exception e) {
            log.error("请求失败{}", e);
            return RUtil.appFail("请求失败!");
        }
    }

    /**
     * 踢下线
     * @param kickOffDto
     * @return 结果
     */
    @PostMapping("/kickOff")
    public String kickOff(KickOffDto kickOffDto) {
        try {
            iGmService.kickOff(kickOffDto);
            return RUtil.appSuccess("成功");
        } catch (BusinessException e) {
            log.error("请求失败{}", e);
            return RUtil.appFail(e.getMessage());
        } catch (Exception e) {
            log.error("请求失败{}", e);
            return RUtil.appFail("请求失败!");
        }
    }

    /**
     * 获取用户数据
     * @return
     */
    @GetMapping("/getUserList")
    public String getUserList(String account) {
        GmUserLoginInfoVo gmUserLoginInfoVo =  iGmService.getUserList(account);
        return RUtil.appSuccess(gmUserLoginInfoVo);
    }


    /**
     * 获取用户道具
     * @return
     */
    @GetMapping("/getUserConfigItem")
    public String getUserConfigItem(String account) {
        List<Bag> bags = iGmService.getUserConfigItem(account);
        return RUtil.appSuccess(bags);
    }

    /**
     * 获取刷新服务器操作记录
     *
     * @return
     */
    @GetMapping("/getRefreshConfigLog")
    public String getRefreshConfigLog( @RequestParam(defaultValue = "1") Integer pageNum,
                                       @RequestParam(defaultValue = "10") Integer pageSize) {
        IPage<RefreshConfigLogVo> refreshConfigLogs = iGmService.getRefreshConfigLog(pageNum,pageSize);
        return RUtil.appSuccess(refreshConfigLogs);
    }

    /**
     * 获取重置活动
     *
     * @return
     */
    @GetMapping("/getResetActivity")
    public String getResetActivity(@RequestParam(defaultValue = "1") Integer pageNum,
                                   @RequestParam(defaultValue = "10") Integer pageSize) {
        IPage<GmConfigActivityStartVo> configActivityStartVos = iGmService.getResetActivity(pageNum,pageSize);
        return RUtil.appSuccess(configActivityStartVos);
    }

    /**
     * 获取用户直播任务列表
     *
     * @return
     */
    @GetMapping("/getWatchLiveTaskList")
    public String getWatchLiveTaskList(String account) {
        GmWatchLiveConfigActivityTaskVo gmUserWatchLiveTaskDetailVos = iGmService.getWatchLiveTaskList(account);
        return RUtil.appSuccess(gmUserWatchLiveTaskDetailVos);
    }

    /**
     * 修改直播任务状态
     *
     * @return
     */
    @PostMapping("/updateWatchLiveTask")
    public String updateWatchLiveTask(@RequestBody GmUserWatchLiveTaskDetailDto gmUserWatchLiveTaskDetailDto) {
        iGmService.updateWatchLiveTask(gmUserWatchLiveTaskDetailDto);
        return RUtil.appSuccess("成功");
    }

} 